package com.meilai.project.service.administration.goods.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.dto.administration.goods.GoodsStorageManageDTO;
import com.meilai.project.entity.administration.goods.GoodsStorageManage;
import com.meilai.project.entity.administration.goods.GoodsType;
import com.meilai.project.mapper.administration.goods.GoodsStorageManageMapper;
import com.meilai.project.service.administration.goods.GoodsStorageManageService;
import com.meilai.project.vo.administration.goods.GoodsStorageManageVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ykb
 * @date 2022/2/13 20:19
 */
@Service
@Log4j2
public class GoodsStorageManageServiceImpl extends ServiceImpl<GoodsStorageManageMapper, GoodsStorageManage> implements GoodsStorageManageService {


    @Override
    public Page<GoodsStorageManageVO> selectPageList(Integer current_page, Integer page_size, GoodsStorageManageDTO searchDTO) {
        Page<GoodsStorageManageVO> page = new Page<>(current_page, page_size);
        List<GoodsStorageManageVO> list = baseMapper.selectPageList(page, searchDTO);

        return page.setRecords(list);
    }

    @Override
    public List<GoodsType> storageList() {
        return baseMapper.storageList();
    }

    //    @Override
//    @Transactional
//    public Boolean editOneById(GoodsStorageManageDTO dto) {
//        GoodsStorageManage temp = getById(dto.getId());
//        if (temp == null) return false;
//        GoodsStorageManage goodsStorageManage = new GoodsStorageManage();
//        BeanUtils.copyProperties(dto, goodsStorageManage);
//        return updateById(goodsStorageManage);
//        return false;
//    }

//    @Override
//    public Boolean updateOrSave(GoodsStorageManageDTO dto) {
//        Long supplier_id = dto.getSupplier_id();
//        if (supplier_id == null) throw new CommonException("缺少供应商信息，无法更新库存");
//        Long supplier_goods_id = dto.getSupplier_goods_id();
//        if (supplier_goods_id == null) throw new CommonException("缺少物品信息，无法更新库存");
//        Long incr_put_number = dto.getPut_number();
//        Long incr_out_number = dto.getOut_number();
//        Long incr_lock_number = dto.getLock_number();
//        List<GoodsStorageManage> list = list(new QueryWrapper<GoodsStorageManage>().isNull("deleted_at").eq("supplier_id",dto.getSupplier_id()).eq("supplier_goods_id", dto.getSupplier_goods_id()));
//        if (list != null && list.size() > 0) {
//            GoodsStorageManage goodsStorageManage = list.get(0);
//            Long id = goodsStorageManage.getId();
//            Long put_number = goodsStorageManage.getPut_number();
//            Long out_number = goodsStorageManage.getOut_number();
//            Long lock_number = goodsStorageManage.getLock_number();
//            Long storage_number = goodsStorageManage.getStorage_number();
//            if(incr_put_number != null && put_number != null){
//                put_number = put_number + incr_put_number;
//            }
//            if(incr_out_number != null && out_number != null){
//                out_number = out_number + incr_out_number;
//            }
//            if(incr_lock_number != null && out_number != null){
//                lock_number = lock_number + incr_lock_number;
//            }
//            storage_number = put_number - out_number;
//            GoodsStorageManage new_goodsStorageManage = new GoodsStorageManage();
//            new_goodsStorageManage.setId(id);
//            new_goodsStorageManage.setSupplier_id(supplier_id);
//            new_goodsStorageManage.setSupplier_goods_id(supplier_goods_id);
//            new_goodsStorageManage.setPut_number(put_number);
//            new_goodsStorageManage.setOut_number(out_number);
//            new_goodsStorageManage.setStorage_number(storage_number);
//            new_goodsStorageManage.setLock_number(lock_number);
//            updateById(new_goodsStorageManage);
//
//        }else{
//            if(incr_put_number == null && incr_out_number != null){
//                //出库
//                throw new CommonException("无入库信息，无法完成出库");
//            }else{
//                GoodsStorageManage new_goodsStorageManage = new GoodsStorageManage();
//                new_goodsStorageManage.setSupplier_id(supplier_id);
//                new_goodsStorageManage.setSupplier_goods_id(supplier_goods_id);
//                new_goodsStorageManage.setPut_number(incr_put_number);
//                new_goodsStorageManage.setOut_number(0L);
//                new_goodsStorageManage.setStorage_number(incr_put_number);
//                new_goodsStorageManage.setLock_number(0L);
//                save(new_goodsStorageManage);
//            }
//        }
//        return true;
//    }
//
//    //校验库存是否足够
//    @Override
//    public Boolean checkGoodsStorage(Long dec_number, Long supplier_id, Long supplier_goods_id) {
//        List<GoodsStorageManage> list = list(new QueryWrapper<GoodsStorageManage>().isNull("deleted_at").eq("supplier_id",supplier_id).eq("supplier_goods_id", supplier_goods_id));
//        if (list != null && list.size() > 0) {
//            GoodsStorageManage goodsStorageManage = list.get(0);
//            Long storage_number = goodsStorageManage.getStorage_number();
//            Long lock_number = goodsStorageManage.getLock_number();
//            if(storage_number !=null && lock_number!=null && dec_number != null && (storage_number-lock_number-dec_number > 0)){
//                return true;
//            }else if(storage_number !=null && lock_number==null && dec_number != null && (storage_number-dec_number > 0)){
//                return true;
//            }else{
//                throw new CommonException("库存量不足，无法完成出库，请联系管理员！");
//            }
//        }
//        return false;
//    }
}
